<?php

namespace App\Http\Controllers;

use App\Http\Biz\SuppOrderBiz;
use App\Http\Biz\SuppReportBiz;
use App\Http\Utils\Common\Consts;
use App\Http\Utils\Common\DictUtils;
use App\Http\Utils\Common\OperResult;
use App\Http\Utils\Common\RoleUtils;
use App\Http\Utils\ControllerTrait\WebUploadTrait;
use GuzzleHttp\Client;
use Illuminate\Http\Request;
use Log;
use ErrorException;

class SuppReportController extends Controller
{
    //根据供应商id查询所有商品
    public function getGoodsBySuppId(Request $request) {
        try {
            Log::debug(get_class($this).' '.__FUNCTION__.' '.'start');
            if (!$request->has('supp_id'))
                return response()->json(['code'=>OperResult::CODE_CREATE_FAIL, 'msg'=>OperResult::CODE_CREATE_FAIL_DESC, 'data'=>[]]);

            $where=$request->input('supp',array());
            $tmpBiz = new SuppReportBiz();
            $data = $tmpBiz->readGoodsByCondition($request->supp_id,$where,$request->account_id);
            Log::debug(get_class($this).' '.__FUNCTION__.' '.'end');
            return response()->json(['code'=>OperResult::CODE_READ_OK, 'msg'=>OperResult::CODE_READ_OK_DESC, 'data'=>$data]);
        } catch (ErrorException $ex) {
            Log::error(get_class($this).' exception '.print_r($ex->getMessage(), true));
            return response()->json(['code'=>OperResult::CODE_READ_FAIL, 'msg'=>OperResult::CODE_READ_FAIL_DESC, 'data'=>[]]);
        }
    }


    //根据goods_id查询商品的规格信息
    public function getSpecMsgByGoodsId(Request $request) {
        try {
            Log::debug(get_class($this).' '.__FUNCTION__.' '.'start');
            if (!$request->has('goods_id'))
                return response()->json(['code'=>OperResult::CODE_CREATE_FAIL, 'msg'=>OperResult::CODE_CREATE_FAIL_DESC, 'data'=>[]]);

            $id=$request->input('goods_id','');
            $tmpBiz = new SuppReportBiz();
            $data = $tmpBiz->getSpecMsgByGoodsId($id);
            Log::debug(get_class($this).' '.__FUNCTION__.' '.'end');
            Log::info(response()->json(['code'=>OperResult::CODE_READ_OK, 'msg'=>OperResult::CODE_READ_OK_DESC, 'data'=>$data]));
            return response()->json(['code'=>OperResult::CODE_READ_OK, 'msg'=>OperResult::CODE_READ_OK_DESC, 'data'=>$data]);
        } catch (ErrorException $ex) {
            Log::error(get_class($this).' exception '.print_r($ex->getMessage(), true));
            return response()->json(['code'=>OperResult::CODE_READ_FAIL, 'msg'=>OperResult::CODE_READ_FAIL_DESC, 'data'=>[]]);
        }
    }



    //上报数据
    public function reportNum(Request $request) {
        try {
            Log::debug(get_class($this).' '.__FUNCTION__.' '.'start');
            Log::info($request->all());
            if (!$request->has('shop'))
                return response()->json(['code'=>OperResult::CODE_CREATE_FAIL, 'msg'=>OperResult::CODE_CREATE_FAIL_DESC, 'data'=>array()]);
            $params = $request->all()['shop'];
            $params = collect($params)->filter(function ($item) {
                return $item != '';
            })->all();
            $tmpBiz = new SuppReportBiz();
            $data = $tmpBiz->reportNumBiz($request->supp_id,$params);
            Log::debug(get_class($this).' '.__FUNCTION__.' '.'end');
            return response()->json(['code'=>OperResult::CODE_READ_OK, 'msg'=>OperResult::CODE_READ_OK_DESC, 'data'=>$data]);
        } catch (ErrorException $ex) {
            Log::error(get_class($this).' exception '.print_r($ex->getMessage(), true));
            return response()->json(['code'=>OperResult::CODE_READ_FAIL, 'msg'=>OperResult::CODE_READ_FAIL_DESC, 'data'=>[]]);
        }
    }

}
